package editor;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.io.File;

public class LevelDialog extends JDialog implements ActionListener
{
	private JTextField name;
	private JTextField width;
	private JTextField height;
	
	private JLabel lblName;
	private JLabel lblWidth;
	private JLabel lblHeight;
	
	private JButton loadTileset;
	private JButton done;
	private JButton cancel;
	
	private TilesetDisplay display;
	
	private Level lvl;
	private String selectedTs;
	
	public LevelDialog( JFrame parent, TilesetDisplay display )
	{
		super( parent, true );
		
		this.setSize( 300, 350 );
		
		name = new JTextField( "Input Name" );
		width = new JTextField( "Input Width" );
		height = new JTextField( "Input Height" );
		
		lblName = new JLabel( "Name:" );
		lblWidth = new JLabel( "Width:" );
		lblHeight = new JLabel( "Height:" );
		
		loadTileset = new JButton( "Load Tileset..." );	
		done = new JButton( "Done" );	
		cancel = new JButton( "Cancel" );
		
		GridBagLayout layout = new GridBagLayout();
		
		addComponent( lblName, 		1, 1, 1, 1, layout );
		addComponent( name, 		2, 1, 2, 1, layout );
		
		addComponent( lblWidth, 	1, 2, 1, 1, layout );
		addComponent( width, 		2, 2, 2, 1, layout );
		
		addComponent( lblHeight, 	1, 3, 1, 1, layout );
		addComponent( height, 	 	2, 3, 2, 1, layout );
	
		addComponent( loadTileset, 	2, 4, 1, 1, layout );
		addComponent( done, 		1, 5, 1, 1, layout );
		addComponent( cancel, 		3, 5, 1, 1, layout );
		
		this.setLayout( layout );
		
		loadTileset.addActionListener( this );
		done.addActionListener( this );
		cancel.addActionListener( this );
		
		this.display = display;
		
		this.setVisible( true );
	}
	
	public Level getLevel()
	{
		return lvl;
	}
	
	private void addComponent( Component c, int col, int row, int width, int height, GridBagLayout layout )
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = col;
		gbc.gridy = row;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = 100;
		gbc.weighty = 100;
		layout.setConstraints( c, gbc );
		gbc.insets = new Insets( 5, 5, 5, 5 );
		this.add( c );
	}
	
	public void actionPerformed( ActionEvent e ) 
	{
		Object o = e.getSource();
		if( o.equals( loadTileset ) )
		{
			JFileChooser fc = new JFileChooser();
			fc.setCurrentDirectory( new File( "C:/Documents and Settings/Owner/Desktop/Projects/Level Editor/TileSets" ) );
			int returnVal = fc.showOpenDialog( this );
			
			if( returnVal == JFileChooser.APPROVE_OPTION )
			{
				selectedTs = fc.getSelectedFile().getAbsolutePath();
			}
		}
		else if( o.equals( done ) )
		{
			int w = Integer.valueOf( width.getText() );
			int h = Integer.valueOf( height.getText() );
			lvl = new Level( name.getText(), w, h, new int[w][h], new int[w][h], new int[w][h], selectedTs );
			
			display.addTileset( lvl.getTileset() );
			
			this.dispose();
		}
		else if( o.equals( cancel ) )
		{
			lvl = null;
			
			this.dispose();
		}
	}
}
